const express = require("express");
const app = express();

app.use(express.urlencoded());
app.use(express.json());

const cors = require("cors");
app.use(cors());

const jwt = require("jsonwebtoken");
var expressJWT = require("express-jwt");

const sercetKey = "yjwBest";

app.post("/login", (req, res) => {
  if (req.body.uname != 'admin' || req.body.pwd != 123456) {
    return res.send({ msg: "用户名或者密码错误" });
  }

  var token = jwt.sign({ uname: req.body.uname }, sercetKey, {
    expiresIn: "10h",
  });

  res.send({
    msg: "登录成功",
    token: token,
  });
});

app.get("/api/home", (req, res) => {
  res.send({ name: "home", msg: "express home" });
});

app.get("/api/news", (req, res) => {
  res.send({ name: "news", msg: "express  news" });
});

app.listen(8080, () => {
  console.log("8080 running   jwt");
});
